home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / prgmming / swag08 / cmdline.swg < prev    next >
Text File  |  1994-09-22  |  8KB  |  1 lines

  1. SWAGOLX.EXE (c) 1993 GDSOFT  ALL RIGHTS RESERVED 00002                                                                           1      08-25-9409:05ALL                      DAVID S. ISSEL           Command Line Parsing     SWAG9408    ö╜a√    30     !}   {πFrom: dissel@nunic.nu.edu (David S. Issel)ππSomeone was looking for command line parsing in turbo pascal.ππThis is a unit that I wrote years ago.ππTo use it, simply put the USES CMDLINE; in your program.ππExample1:  If you entered:  MYPROG /x/y/z="this is a test"ππTurboPascal would respond:  ParamStr(x) Contentsπ                            =========== =================π                            1           /x/y/z="thisπ                            2           isπ                            3           aπ                            4           test"ππMy unit would respond:      1           /Xπ                            2           /Yπ                            3           /Z=this is a testπππExample2:  If you entered:  MYPROG file1,file2,file3ππTurboPascal would respond:  ParamStr(x) Contentsπ                            =========== =================π                            1           file1,file2,file3ππMy unit would respond:      1           FILE1π                            2           FILE2π                            3           FILE3ππMy unit replaces the ParamCount variable and ParamStr() function.πThe original TurboPascal routines are retained as System.ParamCount andπSystem.ParamStr()ππTry it, you'll like it... (I swear!)πππ-------- cut here ------------- cmdline.pas ---------------------π}πUnit CMDLINE;  { Written by David S. Issel, 1989 }π πInterface  { public }π πVar ParamCount:integer;π πFunction ParamStr(Param:word):string;π πImplementation  { private }π πVarπ  ParamArray:array[1..62] of string[127];π πFunction ParamStr;π  beginπ    if Param<=ParamCountπ      then ParamStr:=ParamArray[Param]π      else ParamStr:='';π  end;π πProcedure SetupParamArray;π  varπ    Index:word;π    WorkStr:string;π  procedure TxfrString;π    varπ      SrchChar:string;π    beginπ      SrchChar:=WorkStr[Index];π      Inc(Index);π      while (Index<=Length(WorkStr)) and (WorkStr[Index]<>SrchChar) doπ        beginπ          ParamArray[ParamCount]:=ParamArray[ParamCount]+WorkStr[Index];π          Inc(Index);π        end;π      if Index<=Length(WorkStr)π        then Inc(Index);π    end;π  beginπ    ParamCount:=0;π    if System.ParamCount<1 then Exit;π    WorkStr:=System.ParamStr(1);π    if System.ParamCount>1π      then for Index:=2 to System.ParamCount do π              WorkStr:=WorkStr+' '+System.ParamStr(Index);π    Index:=1;π    repeatπ      Inc(ParamCount);π      ParamArray[ParamCount]:='';π      if (WorkStr[Index]=#34) or (WorkStr[Index]=#39)π        then TxfrStringπ        elseπ          beginπ            if WorkStr[Index]<>','π              then ParamArray[ParamCount]:=ParamArray[ParamCount]+π                                           Upcase(WorkStr[Index]);π            Inc(Index);π            if Index<=Length(WorkStr)π              thenπ                beginπ                  while (Index<=Length(WorkStr)) and (WorkStr[Index]<>#47)π                      and (WorkStr[Index]<>#32) and (WorkStr[Index]<>#34)π                      and (WorkStr[Index]<>#39) and (WorkStr[Index]<>#44)π                    doπ                      beginπ                        ParamArray[ParamCount]:=ParamArray[ParamCount]+π                                                Upcase(WorkStr[Index]);π                        Inc(Index);π                      end;π                  if (Index<=Length(WorkStr)) and ((WorkStr[Index]=#34)π                      or (WorkStr[Index]=#39))π                    then TxfrString;π                end;π          end;π      while (Index<=Length(WorkStr)) and (WorkStr[Index]=#32) doπ        Inc(Index);π    until Index>Length(WorkStr);π  end;π πbegin  { Initialization Code }π  SetupParamArray;πend.π  π                                           2      08-25-9409:05ALL                      RANDALL WOODMAN          Command Line Parsing     SWAG9408    C½    31     !}   {π   CMDPARSE.PASπ   Command line parameter parsingπ   If unit is included, command line is automatically parsed.ππ}πunit CmdParse;πinterfaceππusesπ  Strings;ππtypeππ  CmdPtr = ^CmdRec;π  CmdRec = Recordπ    CmdParam : String[64];  { 64 char to allow for maximum path length }π    Next     : CmdPtr;π  end;ππvarπ  FirstCmd   : CmdPtr;  { Ptr to first CmdRec or Nil }π  CurrentCmd : CmdPtr;  { Ptr to current CmdRec or Nil }π  CmdCnt     : Byte;    { Total of all entered parameters }ππfunction FirstCmdStr : String;π  { Returns the first parameter entered, or the null ('') string. }ππfunction NextCmdStr : String;π  { Returns the next parameter entered, or the null ('') string. }ππfunction ValidCmdStr(ChkStr : String; CaseCheck : Boolean) : Boolean;π  { Checks all input parameters to see if CmdStr has been input.  CaseCheckπ    determines if the the search of parameters should be case sensitive. }ππprocedure ClearCmd;π  { Disposes of all command line pointers except FirstCmd & CurrentCmdπ    which are set to Nil }ππimplementationπ{---------------------------------------------------------------------------}πvarπ  Cmd_I : Byte;ππ  function FirstCmdStr : String;π    { Returns the first parameter entered, or the null ('') string. }π  beginπ    if FirstCmd <> Nil then beginπ      FirstCmdStr := FirstCmd^.CmdParam;π      CurrentCmd := FirstCmd^.Next;π    end elseπ      FirstCmdStr := '';π  end;ππ  function NextCmdStr : String;π    { Returns the next parameter entered, or the null ('') string. }π  beginπ    if CurrentCmd <> Nil then beginπ      NextCmdStr := CurrentCmd^.CmdParam;π      CurrentCmd := CurrentCmd^.Next;π    end elseπ      NextCmdStr := '';π  end;ππ  function ValidCmdStr(ChkStr : String; CaseCheck : Boolean) : Boolean;π    { Checks all input parameters to see if CmdStr has been input.  CaseCheckπ      determines if the the search of parameters should be case sensitive. }π  varπ    CmdStr : String;π    FoundCmd : Boolean;π  beginπ    CmdStr := FirstCmdStr;π    FoundCmd := False;π    repeatπ      if CaseCheck thenπ      beginπ        if CmdStr = ChkStr thenπ          FoundCmd := Trueπ      endπ      elseπ        if (StUpCase(CmdStr) = StUpCase(ChkStr)) thenπ          FoundCmd := True;π      CmdStr := NextCmdStr;π    until CmdStr = '';π    ValidCmdStr := FoundCmd;π  end;ππ  procedure ClearCmd;π    { Disposes of all command line pointers except FirstCmd & CurrentCmdπ      which are set to Nil }π  beginπ    if FirstCmd <> Nil thenπ      repeatπ        CurrentCmd := FirstCmd^.Next;π        Dispose(FirstCmd);π        FirstCmd := CurrentCmd;π      until FirstCmd = Nil;π  end;ππ  procedure CmdAdd(CmdStr : String);π    { Add a new CmdRec to the list }π  varπ    TempCmdPtr : CmdPtr;π  beginπ    New(TempCmdPtr);π    if FirstCmd = Nil thenπ      FirstCmd := TempCmdPtrπ    elseπ      CurrentCmd^.Next := TempCmdPtr;π    TempCmdPtr^.Next := Nil;  { Initialize new Next pointer }π    TempCmdPtr^.CmdParam := CmdStr;π    CurrentCmd := TempCmdPtr;π    Inc(CmdCnt);π  end;ππ  procedure ParsePStr(PStr : String);π    { Parse out a ParamStr() into multiple CmdRecs }π  varπ    WorkStr   : String;π    TempStr   : String;π    SpPos: Byte;π    I,L : Byte;π  beginππ    { translate first - to / }π    if PStr[1] = '-' thenπ      PStr[1] := '/';ππ    SpPos := Pos('/',Copy(PStr,2,Length(PStr)-1));π    if SpPos > 0 thenπ      repeatπ        CmdAdd(Copy(PStr,1,SpPos));π        PStr := Copy(PStr,SpPos+1,Length(PStr)-SpPos);π        SpPos := Pos('/',Copy(PStr,2,Length(PStr)-1));π      until SpPos = 0;π    CmdAdd(PStr);π  end;ππbeginπ  FirstCmd := Nil;π  CurrentCmd := Nil;π  CmdCnt := 0;ππ  for Cmd_I := 1 to ParamCount doπ    ParsePStr(ParamStr(Cmd_I));π  CurrentCmd := FirstCmd;πend.π